package com.hdax.dm.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.hdax.dm.dto.TicketbuyerDto;
import com.hdax.dm.dto.UserDto;
import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.entity.user.DmLinkUser;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.service.LinkUserService;
import com.hdax.dm.service.LoginService;
import com.hdax.dm.utils.Token.TokenUtil;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/order")
public class UserOrderController {

    @Autowired
    private LinkUserService linkUserService;

    @Autowired
    private LoginService loginService;

    private Integer uid(HttpServletRequest request){
        String token= request.getHeader("token");
        Integer id= Integer.valueOf(TokenUtil.parseToken(token));
        return id;
    }

    //用于个人信息页根据用户Id返回用户信息
    @PostMapping("p/querypersoninfo")
    private CommonResponse<UserDto> queryPersonInfo(HttpServletRequest request){
        return loginService.queryPersonInfo(uid(request));
    }

    //根据当前用户返回当前用户常用购票人列表
    @PostMapping("ticketbuyerlist")
    private CommonResponse<List<TicketbuyerDto>> ticketbuyerlist(HttpServletRequest request){
        return linkUserService.ticketbuyerlist(uid(request));
    }

    // 验证购票人是否已经存在
    @PostMapping("validatebuyerexist")
    private  CommonResponse<DmLinkUser> validatebuyerexist(@RequestBody Map<String,String> map){
        return linkUserService.validatebuyerexist(map.get("idCard"));
    }

    @PostMapping("addticketbuyer")
    private  CommonResponse<Integer> addticketbuyer(@RequestBody DmLinkUser dmLinkUser,HttpServletRequest request){
        return linkUserService.addticketbuyer(dmLinkUser,uid(request));
    }

    @PostMapping("deleteticketbuyer")
    private  CommonResponse<Integer> deleteticketbuyer(@RequestBody Map<String,Integer> map){
        return linkUserService.deleteticketbuyer(map.get("linkId"));
    }

    @ExceptionHandler
    private CommonResponse<String> dmException(DmException e){
        return ResponseUtil.returnFail(e);
    }
}
